/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package isvp.implementations;

import isvp.constants.ISVPConstants;
import isvp.exceptions.ComponentException;
import isvp.interfaces.Application;
import isvp.interfaces.ISVP;
import isvp.interfaces.Language;
import isvp.interfaces.Privilege;
import isvp.utilities.ISVPUtils;
import isvp.utilities.XMLUtils;
import java.io.File;
import java.io.IOException;
import java.util.TreeMap;
import java.util.zip.ZipException;
import java.util.zip.ZipFile;
import org.w3c.dom.Document;

/**
 * Implementation for the ISVP interface
 * @author jjophin
 */
class ISVPImpl extends ZipFile implements ISVP {
    protected Document isvXML;
    protected String isvpName;
    protected String isvpUpdateNumber;
    protected Application application;
    protected TreeMap<String, Privilege> privileges;
    protected TreeMap<String, Language> languages;
    
    /**
     * Constructor
     * @param file : File object representing the ISVP file
     * @throws ZipException : if not a proper zip file
     * @throws IOException : if unable to read from the file
     * @throws ISVException : if not a proper ISVP file eg. isv.xml missing
     */
    public ISVPImpl (File file) throws IOException, ComponentException {
        super(file, OPEN_READ);
        isvXML = ISVPUtils.getSAXedComponentDocument(this, ISVPConstants.ISV_XML_FILE, ISVPConstants.ISV_XML_FILE);
        setIsvpName();
        setIsvpUpdateNumber();
        setApplication();
        setPrivileges();
    }

    @Override
    public String getIsvpName() {
        return isvpName;        
    }

    @Override
    public String getIsvpUpdateNumber() {
        return isvpUpdateNumber;
    }
    
    @Override
    public Application getApplication() {
        return application;
    }

    @Override
    public TreeMap<String, Privilege> getPrivileges() {
        return privileges;
    }
    
    @Override
    public TreeMap getLanguages() {
        return languages;
    }
    
    @Override
    public Document getIsvXML() {
        return isvXML;
    }
    
    //Privilege Methods
    /**
     * Returns the name of the administrator privilege
     * @return 
     */
    @Override
    public String getAdministratorPrivilege() {
        String applicationName = application.getApplicationName();
        String adminPrivilegeName = applicationName + " Administrator";
        return adminPrivilegeName;
    }
   
    /**
     * Returns the Privilege object corresponding to the privilege name passed
     * @param privilegeName
     * @return Privilege object
     */
    @Override
    public Privilege getPrivilege(String privilegeName) {
        Privilege privilege = privileges.get(privilegeName);
        return privilege;
    }    
    
    //Language methods
    
    
    /**
     * Sets the isvp name from the isv.xml file
     */
    public void setIsvpName() {
        isvpName = XMLUtils.getTextContent(isvXML, ISVPConstants.ISVP_NAME_XPATH);
    }
    
    /**
     * Sets the isvp update number from the isv.xml file
     */    
    public void setIsvpUpdateNumber() {
        isvpUpdateNumber = XMLUtils.getTextContent(isvXML, ISVPConstants.ISVP_UPDATE_VERSION_XPATH);
    }
    
    /**
     * Sets the application object from the isv.xml file
     */    
    public void setApplication() throws ComponentException{
        application = ISVPFactory.getApplicationImplementation(this);
    }
    
    
    public void setPrivileges() throws ComponentException {
        privileges = ISVPFactory.getPrivilegesImplementation(this);
    }

}
